package net.oschina.app.fragment.tweetsfragments;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.oschina.app.R;
import net.oschina.app.adapter.tweetsadapter.TweetsListAdapter;
import net.oschina.app.bean.News;
import net.oschina.app.bean.Tweet;
import net.oschina.app.bean.TweetsList;
import net.oschina.app.constant.Constants;
import net.oschina.app.fragment.BaseFragment;
import net.oschina.app.listener.OnRecycleViewItemClickListener;
import net.oschina.app.ui.NewsDetailActiviy;
import net.oschina.app.util.URLGet;

import java.util.List;

/**
 * Created by ldl on 2017/4/1.
 */

public class NewFragment extends BaseFragment {

    private XRecyclerView mXRecyclerView;
    private List<Tweet> mTweetslist = null;
    private TweetsListAdapter mTweetsListAdapter;
    private View mView;

    @Override
    protected View initView() {
        // 找到XRecyclerView控件，充气条目在适配器设置
        mView = View.inflate(getContext(), R.layout.base_tweets_fragment, null);
        mXRecyclerView = (XRecyclerView) mView.findViewById(R.id.xrv_base);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mXRecyclerView.setLayoutManager(layoutManager);
        return mView;
    }

    @Override
    protected void setData() {
        String path = Constants.HOST+"/list/tweet_list/page0.xml";
        URLGet urlGet = new URLGet();
        urlGet.getData(path, TweetsList.class, new URLGet.OnURLGetLitener<TweetsList>() {
            @Override
            public void showSuccess(TweetsList bean) {
                mTweetslist = bean.getTweetslist();
                dealCommonSuccess(mTweetslist);
            }

            @Override
            public void showError(Exception e) {
                dealCommonFail();
            }
        });
    }

    @Override
    protected void showSuccess() {
        mTweetsListAdapter = new TweetsListAdapter(mTweetslist);
        mXRecyclerView.setAdapter(mTweetsListAdapter);
        //添加头部
        // initRecyclerHeadView(mRv);
        // 静止下拉刷新，加载更多
        mXRecyclerView.setLoadingMoreEnabled(true);
        mXRecyclerView.setPullRefreshEnabled(false);
        initEvent(); // 初始化事件
        changeView(mView);// 更新处理
    }
    //初始化事件----未实现item点击功能
    private void initEvent() {
        mTweetsListAdapter.setOnItemClickListener(new OnRecycleViewItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                position = position - 1;
                Intent intent = new Intent(getActivity(), XiangqingActivity.class);
                Tweet tweets = mTweetslist.get(position);
                int id = tweets.getId();
                intent.putExtra("id", id);
                startActivity(intent);
            }
        });
    }
}